package com.kiln.juxingqs.fragment

import android.view.View
import androidx.recyclerview.widget.DividerItemDecoration
import client.global.commlib.base.BaseFragment
import client.global.commlib.comm_ui.draw.SpaceItemDecoration
import client.global.commlib.utils.Util
import client.global.commlib.widget.EmptyLayout
import com.kiln.juxingqs.R
import com.kiln.juxingqs.adapter.ZrppAdapter
import com.kiln.juxingqs.api.bean.GoodsIndex
import com.kiln.juxingqs.databinding.FragmentTabtwoBinding
import com.kiln.juxingqs.ui.vm.MainViewModel

/**
 * 商城
 */
class TabTwoFragment : BaseFragment<FragmentTabtwoBinding, MainViewModel>() {

    override fun getViewId(): Int {
        return R.layout.fragment_tabtwo
    }

    val adapter: ZrppAdapter by lazy {
        ZrppAdapter(requireContext())
    }

    override fun initViewModel(): MainViewModel = createViewModel()

    override fun initWidget() {
        binding.recyclerView.adapter = adapter
        binding.recyclerView.addItemDecoration(SpaceItemDecoration(1))
        binding.statusBar.post {
            val h = Util.getStatusBarHeight(requireContext())
            val p = binding.statusBar.layoutParams
            p.height = h
            binding.statusBar.layoutParams = p
            Util.setBg(resources.getColor(R.color.state_bg), requireActivity().window)
        }
        binding.refreshLayout.setOnRefreshListener {
            viewModel.tradeList()
        }
        binding.emptyLayout.setOnClickListener {
            viewModel.tradeList()
        }

        //获取数据
        viewModel.tradeCall.observe(this, {

            it?.let {
                if (it.isSuccess) {
                    adapter.hasMoreState.value = false
                    val list = it.data!!.goods_list as MutableList<GoodsIndex.Goods?>

                    if (list.size > 0) {
                        adapter.submitData(list, it.data!!)
                        adapter.hasMoreState.value = false
                    }
                    binding.emptyLayout.visibility = View.GONE
                } else {
                    if (adapter.itemCount == 0) {
                        binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                    }
                }
                binding.refreshLayout.isRefreshing = false
            }

        })
        if (viewModel.tradeCall.value == null) {
            viewModel.tradeList()
        }

    }

    override fun onResume() {
        super.onResume()
        viewModel.tradeList()
    }


    override fun initData() {

    }

}